package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.xml.internal.utils.ObjectPool;

import dao.IUserDao;
import entity.Users;
import user.util.DButil;

public class UserDaoImpl implements IUserDao{
	int result=-1;
	Connection con=DButil.con;
	PreparedStatement pr=DButil.pr;
	ResultSet st=DButil.st;
	Users users=DButil.users;
	public int login(Users user){//登录
		try {
			String sql="select UID from users where uname=? and upwd=?";
			Object[] params= {user.getUName(),user.getUPWD()};
			st=DButil.Select_All(sql, params);
			if(st.next()) {
				result=st.getInt(1);
			}
			if(result>0) {
				return 1;
			}
			else {
				return 0;
			}
			
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		}finally {
			DButil.Close_All(st, pr, con);
	}
		
	}
	public Boolean Add(Users user) {//增加
			String sql="insert into users(uname,upwd) values(?,?)";
			Object[] params= {user.getUName(),user.getUPWD()};
			return DButil.ExcuteUpdate(sql, params);

	}
	public Boolean Delete(int uid) {//删除
			String sql="delete from users where uid=?";
			Object[] params= {uid};
			return DButil.ExcuteUpdate(sql, params);
	}
	public List<Users> Select_All() {//查询所有
		List<Users> lists= new ArrayList<>();
		Users users=null;
		try {
			String sql="select * from users";
			st=DButil.Select_All(sql, null);
			while(st.next()) {//输出数据
				int UID=DButil.st.getInt("UID");
				String UNAME=st.getString("UNAME");
				String UPWD= st.getString("UPWD");
				users= new Users(UID,UNAME,UPWD);
				lists.add(users);
			}
			return lists;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}finally {
			DButil.Close_All(st, pr, con);
		}
	}
	public Boolean Update(int num,Users user) {//更改
			String sql="update users set uname=?,upwd=? where uid=?";
			Object[] params= {user.getUName(),user.getUPWD(),user.getUID()};
			return DButil.ExcuteUpdate(sql, params);
	}
	public Boolean IsExsist(String name) {//查询是否存在人
		return SelectByID(name)!=null?true:false;
	}
	public boolean IsExsist(int uid) {//查询是否存在人
		return SelectByID(uid)!=null?true:false;
	}
	public Users SelectByID(String name) {//根据Uname查人
		try {
			String sql="select * from users where uname=?";
			Object[] params= {name};
			st=DButil.Select_All(sql,params);
			if(st.next()) {//输出数据
				int UID=st.getInt("UID");
				String UNAME=st.getString("UNAME");
				String UPWD= st.getString("UPWD");
				users= new Users(UID,UNAME,UPWD);
				return users;
			}else {
				return null;
			}
			
			
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}finally {
			DButil.Close_All(st, pr, con);
		}
	}
	public Users SelectByID(int Uid) {// 根据uid查人
		try {
			String sql = "select * from users where uid=?";
			Object[] params= {Uid};
			st=DButil.Select_All(sql, params);
			st = pr.executeQuery();
			if (st.next()) {// 输出数据
				int UID = st.getInt("UID");
				String UNAME = st.getString("UNAME");
				String UPWD = st.getString("UPWD");
				users = new Users(UID, UNAME, UPWD);
				return users;
			} else {
				return null;
			}

		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		} finally {
			DButil.Close_All(st, pr, con);
		}
	}
	public List<Users> SelectAllByPage(int PageIndex,int Pagesize){
		List<Users> userList = new ArrayList<>();
		try {
			String sql="select * from users limit (?-1)*10,?";
			Object[] params= {PageIndex,Pagesize};
			DButil.Select_All(sql, params);
			if (st.next()) {// 输出数据
				int UID = st.getInt("UID");
				String UNAME = st.getString("UNAME");
				String UPWD = st.getString("UPWD");
				users = new Users(UID, UNAME, UPWD);
				userList.add(users);
			}
			return userList;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}finally {
			DButil.Close_All(st, pr, con);
		}
	}
	@Override
	public int GetAllCount() {
		String sql = "select count(*) from users;";
		return DButil.QueryAllCount(sql);
	}
	@Override
	public List<Users> queryusersbypage(int CurrenPage, int PageSize) {
		List<Users> userlist = new ArrayList<>();
		String sql="select * from users limit ?,?;";
		Object[] params = {(CurrenPage-1)*PageSize,PageSize};
		st = DButil.Select_All(sql, params);
		try {
			while(st.next()) {
				users=new Users(st.getInt("UID"),st.getString("UNAME"),st.getString("UPWD"));
				userlist.add(users);
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return userlist;
	}
}
